﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/default")]
    public class Dm_Default_TrafficPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        [Route("traffic", Name = "Getdefault_trafficByPage")]
        public IHttpActionResult GetTraffic(int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            var data = this.Scene.Bll.Dm_Default_Traffic.Get<Dm_Default_Traffic>(null, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("traffic")]
        public HttpResponseMessage PostTraffic(Dm_Default_Traffic entity)
        {
            this.Scene.Bll.Dm_Default_Traffic.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("Getdefault_trafficByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("car", Name = "Getdefault_carByPage")]
        public IHttpActionResult GetCar(int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            var data = this.Scene.Bll.Dm_Default_Traffic.Get<Dm_Default_Asp>(null, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("car")]
        public HttpResponseMessage PostCar(Dm_Default_Asp entity)
        {
            var lrang = entity.Lanerange.Split('~');
            var drang = entity.Directrange.Split('~');

            if (lrang.Length == 1)
            {
                if (entity.Lanecoef != Convert.ToDouble(lrang[0])) throw new Exception("车道系数取值范围为1");
            }

            if (lrang.Length == 2)
            {
                if (!(entity.Lanecoef >= Convert.ToDouble(lrang[0]) && entity.Lanecoef <= Convert.ToDouble(lrang[1]))) 
                    throw new Exception(string.Format("车道系数取值范围为{0}~{1}",lrang[0],lrang[1]));
            }

            if (drang.Length == 2)
            {
                if (!(entity.Directcoef >= Convert.ToDouble(drang[0]) && entity.Directcoef <= Convert.ToDouble(drang[1])))
                    throw new Exception(string.Format("方向系数取值范围为{0}~{1}", drang[0], drang[1]));
            }

            this.Scene.Bll.Dm_Default_Traffic.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("Getdefault_carByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }


        [Route("rating", Name = "Getdefault_ratingByPage")]
        public IHttpActionResult GetRating(int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            var data = this.Scene.Bll.Dm_Default_Traffic.Get<Dm_Default_Rating>(null, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("rating")]
        public HttpResponseMessage PostRating(Dm_Default_Rating entity)
        {
            if (entity.Growth_Rate < entity.Growth_Ratemin || entity.Growth_Rate > entity.Growth_Ratemax) throw new Exception(string.Format("沥青路面增长率取值范围[{0},{1}]",entity.Growth_Ratemin,entity.Growth_Ratemax));
            if (entity.Year_Asp < entity.Year_Aspmin || entity.Year_Asp > entity.Year_Aspmax) throw new Exception(string.Format("沥青路面设计年限取值范围[{0},{1}]", entity.Growth_Ratemin, entity.Growth_Ratemax));

            this.Scene.Bll.Dm_Default_Traffic.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("Getdefault_ratingByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }
    }
}